package net.unix8.client;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.json.JSONException;
import org.json.JSONObject;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import io.netty.channel.ChannelInboundHandlerAdapter;
import net.unix8.share.NetworkShare;

public class ClientHandler extends ChannelInboundHandlerAdapter  {
    private String host;
	private String cmd;
	private int id;
	private ChannelId channelId;
	
	public ClientHandler(ChannelId channelId, String cmd,String host, int id){
		this.cmd = cmd;	
		this.channelId = channelId;
		this.host = host;
		this.id = id;
	}
	
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        //ByteBuf in = (ByteBuf) msg; // (1)
        //byte [] send = ("Hello, I'm client.").getBytes();
        //final ByteBuf buffer = ctx.alloc().buffer(send.length);
        //buffer.writeBytes(send);
        //NetworkShare.clientGroup.find(channelId).writeAndFlush(buffer);
    	/*
        Channel remoteChannel = NetworkShare.clientGroup.find(channelId);
        if(remoteChannel != null)
        	remoteChannel.writeAndFlush(msg);
        //ctx.close();
         * */
    	
    	
		JSONObject studentJSONObject = new JSONObject();  
		try {  
			studentJSONObject.put("host", host);
			studentJSONObject.put("id", id);  
			studentJSONObject.put("cmd", "ctrl"); 
			studentJSONObject.put("status", true);  
			studentJSONObject.put("data", (String)msg);  
		} catch (JSONException e) {  
			e.printStackTrace();  
		}  

		Channel remoteChannel = NetworkShare.clientGroup.find(channelId);
		if(remoteChannel != null){
			final ByteBuf firstMessage;
			firstMessage = Unpooled.buffer(100);	
			firstMessage.writeCharSequence(studentJSONObject.toString(), Charset.forName("UTF-8"));
			remoteChannel.writeAndFlush(firstMessage);
		}

    }
    
    @Override
    public void channelActive(ChannelHandlerContext ctx){
    	InetSocketAddress insocket = (InetSocketAddress) ctx.channel()
				.remoteAddress();
		String clientIP = insocket.getAddress().getHostAddress();
		System.out.print("Establised.ip>> "+clientIP);
		System.out.flush();
    	  byte [] send = cmd.getBytes();
          final ByteBuf buffer = ctx.alloc().buffer(send.length);
          buffer.writeBytes(send);
          ctx.writeAndFlush(buffer);
    }
    
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		InetSocketAddress insocket = (InetSocketAddress) ctx.channel()
				.remoteAddress();
		String clientIP = insocket.getAddress().getHostAddress();
		System.out.print("Disconnect remote host,ip>> "+clientIP);
		System.out.flush();
		NetworkShare.clientGroup.remove(channelId);
		super.channelInactive(ctx);
	}
	
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

	public void setIp(String host) {
		this.host = host;
	}
}